package com.java.day14.homework.puke;

import java.util.*;

public class PukeCardUtils {

    // 声明扑克点数的数组
    public static final String[] POINTS_ARR = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","SJOKER","BJOKER"};

    // 声明扑克花色的数组
    public static final String[] PUKE_TYPE = {"♥","♠","♦","♣"};


    /**
     * 模拟斗地主抓牌
     * @param cards  54张牌
     * @param p1  玩家1的手牌
     * @param p2
     * @param p3
     * @param d1  底牌，剩三张
     */
    public static void drawPukeCard(List<PukeCard> cards, List<PukeCard> p1, List<PukeCard> p2,
                                    List<PukeCard> p3, List<PukeCard> d1){
        //打乱
        Collections.shuffle(cards);
//        System.out.println(cards);
        for (int i = 3; i < cards.size(); i+=3) {

            p1.add(cards.get(i-2));
            p2.add(cards.get(i-1));
            p3.add(cards.get(i));

            if(i>cards.size()-4)
            {
                d1.add(cards.get(i));
                d1.add(cards.get(i+1));
                d1.add(cards.get(i+2));
                break;
            }
        }

    }

    /**
     * 创建一副扑克牌
     * @return
     */
    public static List<PukeCard> createPukeList(){

        ArrayList<PukeCard> pukeCards = new ArrayList<>();

        for (int i = 0; i < POINTS_ARR.length-2; i++) {
            for (String type : PUKE_TYPE) {
                PukeCard pukeCard = new PukeCard(type, POINTS_ARR[i], i);
                pukeCards.add(pukeCard);
            }
        }
        // 单独处理大小王
        PukeCard sjoker = new PukeCard("", "SJOKER", 13);
        PukeCard bjoker = new PukeCard("", "BJOKER", 14);
        pukeCards.add(sjoker);
        pukeCards.add(bjoker);


        return pukeCards;
    }

    //整理手牌
    public static void clearUp(List<PukeCard> p1, List<PukeCard> p2,List<PukeCard> p3){
        Collections.sort(p1,(a,b)->a.getValue()-b.getValue());
        Collections.sort(p2,(a,b)->a.getValue()-b.getValue());
        Collections.sort(p3,(a,b)->a.getValue()-b.getValue());
    }

    //叫地主
    public static void call(ArrayList<PukeCard> p,ArrayList<PukeCard> d){
        p.addAll(d);
    }

    //计算炸弹数量
    public static void bomb(ArrayList<PukeCard> p) {
        HashMap<String, Integer> map = new HashMap<>();
        String key;
        Integer value;
        //炸弹数
        int bombCount = 0;

        for (PukeCard pukeCard : p) {
            key = pukeCard.getPoint();
            value = map.get(key);
            map.put(key, value == null ? 1 : value + 1);
        }
        //王炸
        if ((map.containsKey("SJOKER")&&map.containsKey("BJOKER"))){
            bombCount+=1;
            System.out.print("含王炸");
        }
        //遍历HashMap
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            if(entry.getValue()==4){
                bombCount+=1;
            }
        }
        if(bombCount==0){
            System.out.println("没有炸弹");
        }else{
            System.out.println("共"+bombCount+"个");
        }
    }

    public static void main(String[] args) {
        List<PukeCard> pukeList = createPukeList();
//        for (PukeCard pukeCard : pukeList) {
//            System.out.println(pukeCard);
//        }
//        System.out.println(pukeList.size());


        ArrayList<PukeCard> p1 = new ArrayList<>();
        ArrayList<PukeCard> p2 = new ArrayList<>();
        ArrayList<PukeCard> p3 = new ArrayList<>();
        ArrayList<PukeCard> d1 = new ArrayList<>();

        //抓牌
        drawPukeCard(pukeList,p1,p2,p3,d1);

        //整理手牌
        clearUp(p1,p2,p3);


        System.out.println("玩家1:"+p1);
        System.out.println("玩家2:"+p2);
        System.out.println("玩家3:"+p3);
        System.out.println("底牌:"+d1);


        //叫地主
        System.out.println("玩家1叫地主");
        call(p1,d1);
        System.out.println("地主玩家:"+p1);

        //计算炸弹数量
        System.out.print("玩家1炸弹数量:");
        bomb(p1);
        System.out.print("玩家2炸弹数量:");
        bomb(p2);
        System.out.print("玩家3炸弹数量:");
        bomb(p3);
    }


}
